<?php
class SyntaxController extends CpController
{
    public function actionIndex()
    {
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? $currentPage=1 : $currentPage;
        $numberRecordPerPage = 30;
        
        $search = array();
        
        $syntaxs = ASyntax::GetSyntax($search, $currentPage, $numberRecordPerPage);
        $totalSyntax = ASyntax::GetTotalSyntax($search);
        $numberPage = ceil($totalSyntax / $numberRecordPerPage);
            
        $this->render(
            "index"
            , array(
                "syntaxs"=>$syntaxs                
                , "totalSyntax"=>$totalSyntax
                , "numberPage"=>$numberPage
                , "currentPage"=>$currentPage
            )
        );
    }
    
    public function actionEdit()
    {
        $id = isset($_GET["id"]) ? intval($_GET["id"]) : 0;
        
        /* Lấy thông tin syntax */
        $syntax = ASyntax::getSyntaxInfo($id);
        
        $error = "";
        if(isset($_POST["syntax"])){
            $data = array();
            $data["title"] = trim(strip_tags($_POST["title"]));    
            $data["serviceNumber"] = trim(strip_tags($_POST["serviceNumber"]));    
            $data["syntax"] = trim(strip_tags($_POST["syntax"]));    
            
            $error .= empty($data["title"]) ? "<p>- Bạn chưa nhập tiêu đề</p>" : "";
            $error .= empty($data["serviceNumber"]) ? "<p>- Bạn chưa nhập đầu số</p>" : "";
            $error .= empty($data["syntax"]) ? "<p>- Bạn chưa nhập cú pháp</p>" : "";
            
            if(empty($error)){
                $data["id"] = $id;
                $data["update_date"] = time();
                $data["updater"] = Yii::app()->user->name;
                $result = ASyntax::UpdateSyntax($data);
                if($result>=0){
                    $act = 2;
                    $obj = 18;
                    $content = LogConfig::$action[$act] . "" . LogConfig::$object[$obj] . ": {id: " . $id . ", title: " . $syntax["title"]. ", serviceNumber: " . $syntax["serviceNumber"]. ", syntax: " . $syntax["syntax"] . "} Thành ";
                    $content .= " {id: " . $id . ", title: " . $data["title"] . ", serviceNumber: " . $data["serviceNumber"] . ", syntax: " . $data["syntax"] . "}";
                    $result = AAdminLog::insertAdminLog($act,$obj,$content);
                
                    $this->redirect(Url::createUrl("syntax/index"));
                }
            }
        }
        
        $this->render(
            "edit"
            , array(
                "syntax"=>$syntax                
                , "error"=>$error                
            )
        );
    }
}  
?>
